package com.zmn.plat.admin.controller.service.group;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.stream.Collectors;

import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.google.common.collect.Maps;
import com.zmn.common.constant.GlobalConsts;
import com.zmn.common.utils.office.excel.ExportExcel;
import com.zmn.oms.common.dto.ResultDTO;
import com.zmn.plat.model.entity.category.BaseCategory;
import com.zmn.plat.model.entity.servcategory.ServCategory;
import com.zmn.plat.model.entity.service.group.ServiceGroup;
import com.zmn.plat.model.entity.service.group.ServiceGroupQuery;
import com.zmn.plat.model.entity.service.tariff.ServiceTariff;
import com.zmn.plat.model.entity.service.tariff.ServiceTariffQuery;
import com.zmn.plat.model.vo.service.group.ServiceGroupDetailVO;
import com.zmn.plat.model.vo.service.group.ServiceGroupVO;
import com.zmn.plat.model.vo.service.item.ServiceGroupExportVO;

/**
 * 故障分组表
 *
 * @author tanbiao
 * @since 2019-11-21 18:57:24
 */
@RequestMapping("/service/group/")
@Controller
public class ServiceGroupListController extends ServiceGroupBaseController {
    private final static int EXPORT_EXCEL_SIZE = 10000;

    /**
     * 列表
     *
     * @param query
     * @author tanbiao
     * @since 2019-11-21 18:57:24
     */
    @RequestMapping("list")
    @RequiresPermissions("/service/group/list.action")
    public ModelAndView list(@ModelAttribute ServiceGroupQuery query) {

        List<ServiceGroupVO> faultGroupVOList = faultGroupBService.listGroupVOByQuery(query);
        ServiceTariff faultTariff = faultTariffService.findByKey(query.getTariffId());
        ServCategory servCategory = servCategoryService.findByKey(query.getServCategId());
        BaseCategory baseCategory = baseCategoryService.findByKey(query.getCategOneId());
        ModelAndView mav = new ModelAndView("service/group/list");
        mav.addObject("servCategId", query.getServCategId());
        mav.addObject("categOneId", query.getCategOneId());
        mav.addObject("tariffId", query.getTariffId());
        mav.addObject("faultGroupVOList", faultGroupVOList);
        mav.addObject("faultTariff", faultTariff);
        mav.addObject("servCategory", servCategory);
        mav.addObject("baseCategory", baseCategory);
        return mav;
    }

    /**
     * 复制
     *
     * @param tariffId
     * @author tanbiao
     * @since 2020-03-04 18:57:24
     */
    @RequestMapping("copyGroup")
    @RequiresPermissions("/service/group/copyGroup.action")
    public ModelAndView copyGroup(@RequestParam("tariffId") Integer tariffId, @RequestParam("servCategId") Integer servCategId,
        @RequestParam("categOneId") Integer categOneId) {
        // 价格表：展示的是跟当前价格表同：保内/保外、价格类型、状态：启用的价格表名称
        ServiceTariff currentTariff = faultTariffService.findByKey(tariffId);
        ServiceTariffQuery faultTariffQuery = new ServiceTariffQuery();
        faultTariffQuery.setTariffType(currentTariff.getTariffType());
        faultTariffQuery.setBizType(currentTariff.getBizType());
        faultTariffQuery.setStatus(GlobalConsts.YES);
        List<ServiceTariff> selectTariffs = faultTariffService.listByQuery(faultTariffQuery);
        if (!CollectionUtils.isEmpty(selectTariffs)) {
            // 不能自己拷贝自己
            selectTariffs = selectTariffs.stream().filter(e -> !e.getTariffId().equals(tariffId)).collect(Collectors.toList());
        }
        ModelAndView mav = new ModelAndView("service/group/copy_group");
        mav.addObject("selectTariffs", selectTariffs);
        mav.addObject("tariffId", tariffId);
        mav.addObject("categOneId", categOneId);
        mav.addObject("servCategId", servCategId);
        return mav;
    }

    /**
     * 价格表联动服务分类加载后台产品一级分类,加载分组
     * 
     * @param query
     * @author tanbiao
     * @since 2020-03-04 18:57:24
     */
    @RequestMapping("loadGroup")
    @ResponseBody
    public ResultDTO<List<ServiceGroupDetailVO>> loadGroup(@ModelAttribute ServiceGroupQuery query) {
        // 加载当前价格表下指定的服务分类，后台一级分类下的分组
        List<ServiceGroupDetailVO> servCategories = faultGroupService.listGroupByTariffIdAndServCategIdAndCategOneId(query.getTariffId(),
            query.getServCategId(), query.getCategOneId());
        return ResultDTO.success(servCategories);
    }

    /**
     * 详情
     *
     * @param query
     * @author tanbiao
     * @since 2019-11-21 18:57:24
     */
    @RequestMapping("detail")
    @RequiresPermissions("/service/group/detail.action")
    public ModelAndView detail(@ModelAttribute ServiceGroupQuery query) {
        ServiceGroup faultGroup = faultGroupBService.findByKey(query);
        ModelAndView mav = new ModelAndView("service/group/detail");
        mav.addObject("faultGroup", faultGroup);
        return mav;
    }

    @RequestMapping(value = "exportExcel")
    // @ReadOnlyConnection
    public void exportExcel(HttpServletResponse response, ServiceGroupQuery query) throws Exception {
        // 默认导出10000行
        query.setPageSize(getExportPageLimit(response));
        List<ServiceGroupExportVO> faultExportList = faultGroupService.listExportExcelPageByQuery(query);
        if (CollectionUtils.isEmpty(faultExportList)) {
            faultExportList = new ArrayList<>();
            faultExportList.add(new ServiceGroupExportVO());
        }
        // 价格表ID、价格表名称、价格表类型、保内/保外、服务分类、一级产品分类、项目分组ID、项目分组名称、项目ID、项目名称、以及所有项目的字段（新增新项目页面从上到下的顺序）
        // 按照服务分类、一级分类、项目分组、项目的排序号升序排
        // 封装Excel对象
        LinkedHashMap<String, String> fieldMap = Maps.newLinkedHashMap();

        fieldMap.put("tariffId", "价格表ID");
        fieldMap.put("tariffName", "价格表名称");
        fieldMap.put("tariffType", "价格表类型");
        fieldMap.put("bizType", "业务类型");
        fieldMap.put("servCategName", "服务分类");
        fieldMap.put("categOneName", "一级产品分类");
        fieldMap.put("groupId", "项目分组ID");
        fieldMap.put("groupName", "项目分组名称");
        fieldMap.put("itemId", "项目ID");
        fieldMap.put("itemName", "项目名称");
        fieldMap.put("categName", "项目类型名称");
        fieldMap.put("unit", "单位");
        fieldMap.put("priceStandard", "一口价");
        fieldMap.put("priceSplit", "区分明细");
        fieldMap.put("trafficFee", "交通费");
        fieldMap.put("hourFee", "工时费");
        fieldMap.put("serviceRatio", "技术服务费");
        fieldMap.put("partRatio", "配件包干费%");
        fieldMap.put("countLimit", "最大数量");
        fieldMap.put("sort", "排序号");
        fieldMap.put("desc", "项目说明");
        fieldMap.put("status", "状态");
        fieldMap.put("general", "默认项目");
        fieldMap.put("mutexMode", "互斥性");
        fieldMap.put("cityLimit", "城市系数");
        fieldMap.put("brandLimit", "品牌系数");
        fieldMap.put("manualPrice", "修改价格");
        fieldMap.put("editEnable", "APP编辑");
        fieldMap.put("brandLimit", "APP显示");
        fieldMap.put("show", "工程师拍照");

        ExportExcel<ServiceGroupExportVO> excel = new ExportExcel<>(faultExportList, fieldMap, true);
        excel.exportExcel(response, "项目管理");
    }
}
